package cn.ztuo.bitrade.config;

import cn.ztuo.bitrade.Trade.CoinTrader;
import cn.ztuo.bitrade.Trade.CoinTraderFactory;
import cn.ztuo.bitrade.entity.ContractCoin;
import cn.ztuo.bitrade.service.ContractCoinService;
import cn.ztuo.bitrade.service.ContractOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.List;

@Slf4j
@Configuration
public class CoinTraderConfig {

    /**
     * 配置交易处理类
     * @param exchangeCoinService
     * @param kafkaTemplate
     * @return
     */
    @Bean
    public CoinTraderFactory getCoinTrader(ContractCoinService exchangeCoinService, KafkaTemplate<String,String> kafkaTemplate, ContractOrderService exchangeOrderService){
        CoinTraderFactory factory = new CoinTraderFactory();
        List<ContractCoin> coins = exchangeCoinService.findAllEnabled();
        for(ContractCoin coin:coins) {
            log.info("init trader,symbol={}",coin);
            //创建交易对对象
            CoinTrader trader = new CoinTrader(coin);
            //设置kafka对象
            trader.setKafkaTemplate(kafkaTemplate);
            //设置价格精度
            trader.setBaseCoinScale(coin.getBaseCoinScale());
            //设置数量精度
            trader.setCoinScale(coin.getCoinScale());
            //设置停止的交易
            trader.stopTrading();
            //设置初始化交易线程
            trader.initialize();
            //往CoinTraderFactory添加 交易对  交易的对象
            factory.addTrader(coin.getSymbol(),trader);
        }
        return factory;
    }

}
